package com.unknown.explorer;

import com.threed.jpct.Logger;
import com.unknown.explorer.MultiTouchController.MultiTouchObjectCanvas;
import com.unknown.explorer.MultiTouchController.PointInfo;
import com.unknown.explorer.MultiTouchController.PositionAndScale;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class myGLSurfaceView extends GLSurfaceView implements MultiTouchObjectCanvas<Object>{

	private float touchTurn = 0;
	private float touchTurnUp = 0;
	private float xpos = -1;
	private float ypos = -1;
	
	private Handler h_gameLogic;
	
	private MultiTouchController<Object> multiTouchController;
	
	private int[] mTouchPointColors = new int[MultiTouchController.MAX_TOUCH_POINTS];
	
	//current touch point info
	private PointInfo mCurrTouchPoint;
	
	//paint for drawing touch circles
	private Paint mLinePaintCrossHairs = new Paint();
	
	//dummy object
	Object dummyObj = new Object();
	
	public myGLSurfaceView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		multiTouchController = new MultiTouchController<Object>(this);
		
		mLinePaintCrossHairs.setColor(Color.BLUE);
		mLinePaintCrossHairs.setStrokeWidth(5);
		mLinePaintCrossHairs.setStyle(Style.STROKE);
		mLinePaintCrossHairs.setAntiAlias(true);
		
		mCurrTouchPoint = new PointInfo();
	}
	public myGLSurfaceView(Context context, AttributeSet attrs) {
		super( context, attrs );
		multiTouchController = new MultiTouchController<Object>(this);
		
		mLinePaintCrossHairs.setColor(Color.BLUE);
		mLinePaintCrossHairs.setStrokeWidth(5);
		mLinePaintCrossHairs.setStyle(Style.STROKE);
		mLinePaintCrossHairs.setAntiAlias(true);
		
		mCurrTouchPoint = new PointInfo();
	}
	
	public void setHandlerGameLogic(Handler h)
	{
		h_gameLogic = h;
	}
	
	public boolean onTouchEvent(MotionEvent me)
	{
//		// MotionEvent reports input details from the touch screen
//	    // and other input controls. In this case, you are only
//	    // interested in events where the touch position changed.
//		
//		if (me.getAction() == MotionEvent.ACTION_DOWN) {
//			xpos = me.getX();
//			ypos = me.getY();
//			
//			gameMessage sentMsg = new gameMessage();
//			
//			sentMsg.myData.put("xpos", xpos);
//			sentMsg.myData.put("ypos", ypos);
//			sentMsg.messageType = "touch down";
//			
//			Message m = new Message();
//			m.obj = sentMsg;
//			h_gameLogic.sendMessage(m);
//			
//			return true;
//		}
//
//		if (me.getAction() == MotionEvent.ACTION_UP) {
//			xpos = -1;
//			ypos = -1;
//			touchTurn = 0;
//			touchTurnUp = 0;
//			
//			gameMessage sentMsg = new gameMessage();
//			
//			sentMsg.myData.put("xpos", me.getX());
//			sentMsg.myData.put("ypos", me.getY());
//			sentMsg.messageType = "touch up";
//			
//			Message m = new Message();
//			m.obj = sentMsg;
//			h_gameLogic.sendMessage(m);
//			
//			return true;
//		}
//
//		if (me.getAction() == MotionEvent.ACTION_MOVE) {
//			float xd = me.getX() - xpos;
//			float yd = me.getY() - ypos;
//
//			xpos = me.getX();
//			ypos = me.getY();
//
//			touchTurn = xd / -1f;
//			touchTurnUp = yd / -1f;
//			
//			gameMessage sentMsg = new gameMessage();
//			
//			sentMsg.myData.put("touchTurn", touchTurn);
//			sentMsg.myData.put("touchTurnUp", touchTurnUp);
//			sentMsg.myData.put("xpos", me.getX());
//			sentMsg.myData.put("ypos", me.getY());
//			sentMsg.messageType = "touch turn";
//			
//			Message m = new Message();
//			m.obj = sentMsg;
//			h_gameLogic.sendMessage(m);
//			
//			return true;
//		}
//        	
//		return true;
		
		return multiTouchController.onTouchEvent(me);
	}
	
	//MultiTouchController implementations
	public Object getDraggableObjectAtPoint(PointInfo touchPoint) {
		// TODO Auto-generated method stub

		mCurrTouchPoint.set(touchPoint);
	
//		Logger.log("point coordinates:" +  Float.toString(touchPoint.getX()) + ", " + Float.toString(touchPoint.getY()));
//		
//		float xd = touchPoint.getX() - xpos;
//		float yd = touchPoint.getY() - ypos;
//
//		xpos = touchPoint.getX();
//		ypos = touchPoint.getY();
//
//		touchTurn = xd / -1f;
//		touchTurnUp = yd / -1f;
//		
//		gameMessage sentMsg = new gameMessage();
//		
//		sentMsg.myData.put("touchTurn", touchTurn);
//		sentMsg.myData.put("touchTurnUp", touchTurnUp);
//		sentMsg.myData.put("xpos", xpos);
//		sentMsg.myData.put("ypos", ypos);
//		sentMsg.messageType = "touch turn";
//		
//		Message m = new Message();
//		m.obj = sentMsg;
//		h_gameLogic.sendMessage(m);
		
//		return null;
		return dummyObj;
	}
	public void getPositionAndScale(Object obj,
			PositionAndScale objPosAndScaleOut) {
		// TODO Auto-generated method stub		
	}
	public boolean setPositionAndScale(Object obj,
			PositionAndScale newObjPosAndScale, PointInfo touchPoint) {
		// TODO Auto-generated method stub
		
		mCurrTouchPoint.set(touchPoint);
		
		Logger.log("point coordinates:" +  Float.toString(touchPoint.getX()) + ", " + Float.toString(touchPoint.getY()));
		
		float xd = touchPoint.getX() - xpos;
		float yd = touchPoint.getY() - ypos;

		xpos = touchPoint.getX();
		ypos = touchPoint.getY();

		touchTurn = xd / -1f;
		touchTurnUp = yd / -1f;
		
		gameMessage sentMsg = new gameMessage();
		
		sentMsg.myData.put("touchTurn", touchTurn);
		sentMsg.myData.put("touchTurnUp", touchTurnUp);
		sentMsg.myData.put("xpos", xpos);
		sentMsg.myData.put("ypos", ypos);
		sentMsg.messageType = "touch turn";
		
		Message m = new Message();
		m.obj = sentMsg;
		h_gameLogic.sendMessage(m);
		
		return false;
	}
	public void selectObject(Object obj, PointInfo touchPoint) {
		// TODO Auto-generated method stub
		
		gameMessage sentMsg = new gameMessage();
		
		if(obj == null)
		{
			Logger.log("testing: touch up!");
			sentMsg.myData.put("touch status", "up");
			sentMsg.messageType = "touch updown";
		}
		else
		{
			Logger.log("testing: touch down");
			sentMsg.myData.put("touch status", "down");
			sentMsg.messageType = "touch updown";
		}

		Message m = new Message();
		m.obj = sentMsg;
		h_gameLogic.sendMessage(m);
	}
	

}
